<?php
class Login extends CI_Controller
{

	function __construct()
	{
		parent::__construct();
		
		$this->load->library('Form_validation');
		
	}

	function index()
	{
		if ( ! $this->user_login->is_logged_in() )
		{
			$val = $this->form_validation;
			
			// Set form validation rules
			$val->set_rules('username', 'Username', 'trim|required|xss_clean');
			$val->set_rules('password', 'Password', 'trim|required|xss_clean');
			$val->set_rules('remember', 'Remember me', 'integer');

			// 检查 登录尝试次数是否超过了在 配置文件中指定的值 并执行验证码
			if ($this->user_login->is_max_login_attempts_exceeded())
			{
				$val->set_rules('captcha', 'Confirmation Code', 'trim|required|xss_clean|callback_captcha_check');
			}
				
			if ($val->run() AND $this->user_login->login($val->set_value('username'), $val->set_value('password'), $val->set_value('remember')))
			{
				// Redirect to homepage
				redirect('', 'location');
			}

			else
			{
				//被踢用户
				if ($this->user_login->is_banned())
				{
					// 跳转 uri
					$this->user_login->deny_access('banned');
				}
				else
				{						
					// 配置默认不显示验证码
					$data['show_captcha'] = FALSE;
				
					// 如果登录尝试超过最大尝试配置 显示验证码
					if ($this->user_login->is_max_login_attempts_exceeded())
					{
						// 创建验证码					
						$this->user_login->captcha();
						$data['show_captcha'] = TRUE;
					}
					
					// Load login page view
					$this->load->view($this->user_login->login_view, $data);
				}
			}

		}
		else
		{
			$this->load->library('category');
			$data['cate'] = $this->category->getListStr(0, false, 0);
			$data['cate_health'] = $this->category->getListStr(0, false, 0, false, 3, 3);
			$this->load->view('view',$data);
		}
	}
	
	function logout()
	{
		$this->user_login->logout();
		redirect('', 'location');
	}
	
	


}

/* End of file user.php */
/* Location: ./application/controllers/user.php */